package ntd.util.frame.task.handle.fm;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ToolOpt;
import com.modular.iot.bo.IotData;
import ntd.task.Task;
import ntd.task.bo.TaskInfo;
import ntd.util.frame.task.handle.fm.remote.RemoteFmProvincePlatform;
import ntd.util.frame.task.handle.fm.remote.bo.ProPlatSiteInfo;
import ntd.util.frame.task.handle.fm.remote.bo.RemoteFmProvincePlatformInBo;

import java.util.List;

/**
 * 省平台数据同步任务，理论 1小时5分钟执行一次
 */
public class FmProvincePlatformDataSync implements Task {

    @Override
    public TaskInfo info() {
        return new TaskInfo("富铭-省平台数据同步", "task-fm-province-platform-data-sync", new FmProvincePlatformDataSync());
    }

    @Override
    public void execute(Object o) {
        // 查询基站信息
        List<ProPlatSiteInfo> proPlatSiteInfos = RemoteFmProvincePlatform.siteInfo();
        // 同步基站信息
        for (ProPlatSiteInfo proPlatSiteInfo : proPlatSiteInfos) {
            // 初始化基站信息
            ToolOpt.OPT_T_DEVICE.initDevice(proPlatSiteInfo.getStationId(), "127.0.0.1");

            // 查询基站数据 - 文档提示 - 间隔一小时
            RemoteFmProvincePlatformInBo remoteFmProvincePlatformInBo = new RemoteFmProvincePlatformInBo();
            remoteFmProvincePlatformInBo.setToTime(DateUtil.now());
            remoteFmProvincePlatformInBo.setFromTime(DateUtil.formatDateTime(DateUtil.date().offset(DateField.HOUR_OF_DAY, -2)));
            remoteFmProvincePlatformInBo.setStationId(proPlatSiteInfo.getStationId());
            List<IotData> iotData = RemoteFmProvincePlatform.listHistoryHourData(remoteFmProvincePlatformInBo);

            // 存入基站数据
            iotData.forEach(ToolOpt.OPT_T_DATA::saveIotData);
        }
    }
}
